<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <!-- 搜索 -->
      <el-input v-model="query.value" clearable placeholder="输入名称搜索" maxlength="10" style="width: 200px;"
                class="filter-item" @keyup.enter.native="init"/>
      <el-button class="filter-item" size="mini" type="success" icon="el-icon-search" @click="init">搜索</el-button>
      <!-- 新增 -->
      <el-button
        class="filter-item"
        size="mini"
        type="primary"
        icon="el-icon-plus"
        @click="add">新增
      </el-button>
    </div>
    <eForm ref="form" :is-add="isAdd"/>
    <el-table
      :data="data"
      style="width: 100%;margin-bottom: 20px;"
      row-key="id"
      border
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}">
      <el-table-column
        prop="sort"
        label="排序号"
        width="100">
      </el-table-column>
      <el-table-column
        prop="label"
        label="专业名称">
      </el-table-column>
      <el-table-column label="操作" width="150" align="center">
        <template slot-scope="scope">
          <el-button size="mini" type="primary" icon="el-icon-edit" @click="edit(scope.row)"/>
          <el-popover
            :ref="scope.row.id"
            placement="top"
            width="180">
            <p>确定删除本条数据吗？</p>
            <div style="text-align: right; margin: 0">
              <el-button size="mini" type="text" @click="$refs[scope.row.id].doClose()">取消</el-button>
              <el-button :loading="delLoading" type="primary" size="mini" @click="subDelete(scope.row.id)">确定
              </el-button>
            </div>
            <el-button slot="reference" type="danger" icon="el-icon-delete" size="mini"/>
          </el-popover>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
  import eForm from './from'
  import { tree, del } from '@/api/framework/system/specialty'

  export default {
    components: { eForm },
    data() {
      return {
        isAdd: true,
        loading: false,
        delLoading: false,
        data: [],
        query: {
          value: ''
        }
      }
    },
    created() {
      this.$nextTick(() => {
        this.init()
      })
    },
    methods: {
      init() {
        const params = { specialtyName: this.query.value }
        tree(params).then(res => {
          this.data = res
        })
      },
      add() {
        this.isAdd = true
        this.$refs.form.getSpecialtyType()
        this.$refs.form.dialog = true
      },
      edit(row) {
        this.isAdd = false
        this.$refs.form.getSpecialtyType()
        this.$refs.form.form = {
          id: row.id,
          label: row.label,
          pid: row.pid,
          sort: row.sort
        }
        this.$refs.form.dialog = true
      },
      subDelete(id) {
        this.delLoading = true
        del(id).then(res => {
          this.$notify({
            title: '删除成功',
            type: 'success',
            duration: 2500
          })
          this.init()
        })
        this.delLoading = false
        this.$refs[id].doClose()

      }
    }
  }
</script>
